#include "net_context.hpp"
#include <iostream>
#include <cstring>

using namespace event_framework;

class TcpServer
{
public:
    TcpServer(EventLoop& loop_, const EndPoint& ep)
        : service(loop, ep) {}
    void Start()
    {
        netCtx.Init();
        TcpService& service = netCtx.GetTcpService();
        service.SetReadableCallback(std::bind(&TcpServer::OnMessage, this, std::placeholders::_1));
        service.SetWriteableCallback(std::bind(&TcpServer::Echo, this, std::placeholders::_1));
        service.Listen();
    }
    void Stop()
    {
        service.Fini();
    }
private:
    void OnMessage(const Socket& socket)
    {
        char buf[64];
        bzero(buf, sizeof buf);
        socket.Recv(buf, sizeof buf);
        std::cout << "get request:" << buf << endl;
    }
    void Echo(const Socket& socket)
    {
        char buf[] = "response";
        socket.Send(buf, sizeof buf);
    }
    NetContext netCtx;
};



int main()
{
    EventLoop loop;
    TcpServer server(loop, EndPoint("127.0.0.1", 6061));
    server.Start();
    loop.Loop();
    std::cin.get();
    return 0;
}